#!/bin/bash

echo "----------------------------------------"

list=(10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022)
for port in ${list[*]}; do

  # 获取端口为 10011 的PID
  PID=$(netstat -tunlp | grep "$port" | awk '{print $7}')

  # 检测字符串长度是否为0
  if [ -z "$PID" ]; then
    PID="1/"
  fi
  # 查找字符'/'的位置
  index=$(expr index "$PID" /)
  index=$(expr "$index" - 1)

  # 提取子字符串
  PID=$(echo $PID | cut -b 1-"$index")
  echo "--- port=$port --- PID=$PID"

  if [ '1' -eq "$PID" ]; then
    echo "--- port not started"
  else
    kill -9 "$PID"
    echo "--- port kill success"
  fi

  echo "----------------------------------------"
done

